﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;

namespace Drawer.Controls
{
    /// <summary>
    /// Interaction logic for ColorPicker.xaml
    /// </summary>
    public partial class ColorPicker : UserControl
    {
        public event Action<Brush> SelectedColorChanged;
        public static RoutedUICommand SelectColorCommand = new RoutedUICommand("SelectColorCommand", "SelectColorCommand", typeof(ColorPicker));

        private Brush selectedColor;
        public Brush SelectedColor
        {
            get
            {
                return selectedColor;
            }
            set
            {
                selectedColor = value;
                colorRect.Fill = value;
                if (SelectedColorChanged != null)
                {
                    SelectedColorChanged(value);
                }
            }
        }

        public ColorPicker(Brush defaultColor, string label)
        {
            InitializeComponent();
            CommandBindings.Add(new CommandBinding(SelectColorCommand, SelectColorCommandExecute));

            SelectedColor = defaultColor;
            colorPickerLabel.Text = label;
        }

        private void SelectColorCommandExecute(object sender, ExecutedRoutedEventArgs e)
        {
            SelectedColor = new SolidColorBrush((Color)ColorConverter.ConvertFromString(e.Parameter.ToString()));
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            colorPopup.IsOpen = false;
            e.Handled = false;
        }

        private void Button_Transparent_Click(object sender, RoutedEventArgs e)
        {
            SelectedColor = null;
            colorPopup.IsOpen = false;
        }
    }
}
